package com.huluobo.a2202monthdemo.video

import android.animation.Animator.AnimatorListener
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationUtils
import android.widget.EditText
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2202monthdemo.R
import com.huluobo.a2202monthdemo.adapter.CommentAdapter
import com.huluobo.a2202monthdemo.adapter.VideoAdapter
import com.huluobo.a2202monthdemo.databinding.FragmentVideoBinding
import com.huluobo.base.bean.CommentBean
import com.huluobo.base.bean.VideoBean
import com.huluobo.base.view.BaseMVVMFragment
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

class VideoFragment : BaseMVVMFragment<FragmentVideoBinding>() {
    private lateinit var viewModel: VideoViewModel
    private lateinit var videoAdapter: VideoAdapter
    private val videoList = mutableListOf<VideoBean>()

    private var currentPosition: Int = 0

    private lateinit var player: StandardGSYVideoPlayer

    private lateinit var commentAdapter: CommentAdapter
    private val commentList = mutableListOf<CommentBean>()
    override fun getLayoutId(): Int = R.layout.fragment_video

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[VideoViewModel::class.java]

        viewModel.getVideoList(1, 10)

        viewModel.getVideoListSuccess.observe(this) {
            if (it.code == 200) {
                videoList.clear()
                videoList.addAll(it.data)
                videoAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getVideoListFailed.observe(this) {
            Log.i("TagA", "获取视频列表失败:$it")
        }

        viewModel.likeVideoSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            if (it.code == 200) {
                videoAdapter.data[currentPosition].dianzan = 1
                videoAdapter.notifyDataSetChanged()
            }
        }

        viewModel.likeVideoFailed.observe(this) {
            ToastUtils.showLong(it)
        }

        viewModel.noLikeVideoSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            if (it.code == 200) {
                videoAdapter.data[currentPosition].dianzan = 0
                videoAdapter.notifyDataSetChanged()
            }
        }

        viewModel.noLikeVideoFailed.observe(this) {
            ToastUtils.showLong(it)
        }

        viewModel.followAuthSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            if (it.code == 200) {
                videoAdapter.data[currentPosition].guanzhu = 1
                videoAdapter.notifyDataSetChanged()
            }
        }

        viewModel.followAuthFailed.observe(this) {
            Log.i("TagA", "关注失败:$it")
        }

        viewModel.getCommentListSuccess.observe(this) {
            if (it.code == 200) {
                commentList.clear()
                commentList.addAll(it.data)
                commentAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getCommentListFailed.observe(this) {
            Log.i("TagA", "获取评论列表失败:$it")
        }

        viewModel.sendCommentSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            if (it.code == 200) {
                commentAdapter.data.addAll(0, it.data)
                commentAdapter.notifyDataSetChanged()
                val editText = videoAdapter.getViewByPosition(currentPosition, R.id.item_video_input_et) as EditText
                editText.text.clear()
            }
        }

        viewModel.sendCommentFailed.observe(this) {
            Log.i("TagA", "发送评论失败:$it")
        }

        viewModel.collectVideoSuccess.observe(this) {
            ToastUtils.showLong(it)
        }

        viewModel.collectVideoFailed.observe(this) {
            ToastUtils.showLong(it)
        }
    }

    override fun initView() {
        val linearLayoutManager = LinearLayoutManager(requireContext())
        dataBinding.videoRv.layoutManager = linearLayoutManager
        videoAdapter = VideoAdapter(videoList)
        dataBinding.videoRv.adapter = videoAdapter
        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(dataBinding.videoRv)

        dataBinding.videoRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                currentPosition = linearLayoutManager.findFirstVisibleItemPosition()

                player =
                    videoAdapter.getViewByPosition(currentPosition, R.id.item_video_gsyPlayer) as StandardGSYVideoPlayer

//                player.startPlayLogic()
            }
        })

        videoAdapter.addChildClickViewIds(
            R.id.item_video_like_iv,
            R.id.item_video_add_iv,
            R.id.item_video_comment_iv,
            R.id.item_video_close_iv,
            R.id.item_video_send_iv,
            R.id.item_video_collect_iv
        )

        videoAdapter.setOnItemChildClickListener { adapter, view, position ->
            val videoBean = adapter.data[position] as VideoBean

            when (view.id) {
                R.id.item_video_like_iv -> likeVideo(videoBean)
                R.id.item_video_add_iv -> followAuth(videoBean)
                R.id.item_video_comment_iv -> openComment(videoBean)
                R.id.item_video_close_iv -> closeComment()
                R.id.item_video_send_iv -> sendComment(videoBean)
                R.id.item_video_collect_iv -> collectVideo(videoBean)
            }
        }
    }

    private fun collectVideo(videoBean: VideoBean) {
        viewModel.collectVideo(videoBean)
    }

    private fun sendComment(videoBean: VideoBean) {
        val editText = videoAdapter.getViewByPosition(currentPosition, R.id.item_video_input_et) as EditText
        val sendMessage = editText.text.toString().trim()
        if (!TextUtils.isEmpty(sendMessage)) {
            viewModel.sendComment(videoBean.id, sendMessage)
        } else {
            ToastUtils.showLong("请输入评论内容")
        }
    }

    private fun closeComment() {
        val commentLayout = videoAdapter.getViewByPosition(currentPosition, R.id.item_video_comment_ll)
        if (commentLayout != null) {
            val animOut = AnimationUtils.loadAnimation(requireContext(), R.anim.anim_out)
            animOut.duration = 500
            animOut.interpolator = AccelerateInterpolator()
            animOut.setAnimationListener(object : AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {
                    commentLayout.visibility = View.GONE
                    dataBinding.videoRv.setIntercept(true)
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }
            })
            commentLayout.startAnimation(animOut)
        }
    }

    private fun openComment(videoBean: VideoBean) {
        dataBinding.videoRv.setIntercept(false)
        val commentLayout = videoAdapter.getViewByPosition(currentPosition, R.id.item_video_comment_ll)
        if (commentLayout != null) {
            commentLayout.visibility = View.VISIBLE
            val animIn = AnimationUtils.loadAnimation(requireContext(), R.anim.anim_in)
            animIn.duration = 500
            animIn.interpolator = AccelerateInterpolator()
            animIn.setAnimationListener(object : AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                    //加载评论列表
                    viewModel.getCommentList(videoBean.id)
                }

                override fun onAnimationEnd(animation: Animation?) {
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }
            })
            commentLayout.startAnimation(animIn)

            val commentRv: RecyclerView =
                videoAdapter.getViewByPosition(currentPosition, R.id.item_video_comment_rv) as RecyclerView
            commentRv.layoutManager = LinearLayoutManager(requireContext())
            commentAdapter = CommentAdapter(commentList)
            commentRv.adapter = commentAdapter
        }
    }

    private fun followAuth(videoBean: VideoBean) {
        viewModel.followAuth(videoBean.authname)
    }

    private fun likeVideo(videoBean: VideoBean) {
        if (videoBean.dianzan == 0) {
            viewModel.likeVideo(videoBean.id)
        } else {
            viewModel.noLikeVideo(videoBean.id)
        }
    }
}